﻿using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

namespace Game
{
    //-----------------------------------------------------------------------------------------------------------
    //                                                              场景管理
    //一种古老的管理方法。
    //_______________________________________________________________________________________________
    /// <summary>
    /// 场景管理类，用于解锁、读取下一关卡
    /// </summary>
    class SceneControllerBase
    {
        //当前已经解锁的场景数
        //static int g_scenesUnlocked = 1;
        static bool[] g_scenesUnlocked;
        static int g_CurrSceneID = 0;                                             //当前场景序号,从0 开始:0为开始场景
        public static int CurrScene { get { return g_CurrSceneID; } }
        const string STR_SCENE_UNLOCKED = "SLOT1_SU";                               //保存时save到PlayerPref的位置

        /// <summary>
        /// 初始化
        /// </summary>
        protected static void Init()
        {
            //初始化解锁关卡的数组
            g_scenesUnlocked = new bool[SceneManager.sceneCountInBuildSettings];
            g_scenesUnlocked[0] = true;

            ReadData();
        }

        public static void ReadData()
        {
            //读取字符串
            string strRead = PlayerPrefs.GetString(STR_SCENE_UNLOCKED, null);

            if (strRead != null)
            {
                //尝试解析里面的内容
                string[] levelsPsw = strRead.Split(';');
                //print("read:" + strRead);
                for (int i = 0; i < levelsPsw.Length; i++)
                {
                    if (i >= MaxScenes)
                    {
                        return;
                    }

                    int secret = 0;
                    int.TryParse(levelsPsw[i], out secret);
                    if (GetPswForLevel(i) == secret)
                    {
                        //密码正确
                        g_scenesUnlocked[i] = true;
                    }
                }
            }
        }

        private static int GetPswForLevel(int lv)
        {
            int r = 180;
            System.Random random = new System.Random(r + lv);
            for (int i = 0; i < lv + 1; i++)
            {
                r = random.Next(100);
            }
            return r;
        }

        public static void SaveData()
        {
            //拼接密码
            string psw = "";

            for (int i = 0; i < g_scenesUnlocked.Length; i++)
            {
                int currPsw = 0;
                if (g_scenesUnlocked[i])
                {
                    currPsw = GetPswForLevel(i);
                }
                psw = psw + currPsw.ToString() + ';';
            }
            PlayerPrefs.SetString(STR_SCENE_UNLOCKED, psw);
        }

        /// <summary>
        /// 获得当前已经解锁的场景数
        /// </summary>
        public static int ScenesUnlockedCount
        {
            get
            {
                int i;
                for (i = 0; i < g_scenesUnlocked.Length; i++)
                {
                    //Debug.Log("g-pos: " + i + "is :" + g_scenesUnlocked[i]);
                    if (g_scenesUnlocked[i] == false)
                        break;
                }

                return i;
            }
        }

        /// <summary>
        /// 获得程序最大场景数
        /// </summary>
        public static int MaxScenes { get { return SceneManager.sceneCountInBuildSettings; } }

        /// <summary>
        /// 读取某个指定关卡
        /// </summary>
        /// <param name="id"></param>
        public static void LoadScene(int id)
        {
            //Debug.Log ( "load scene:" + id );
            if (id >= 0 && id < MaxScenes)
            {
                g_CurrSceneID = id;
                SceneManager.LoadScene(id);
            }
        }

        /// <summary>
        /// 尝试解锁下一场景，成功返回true
        /// </summary>
        public static bool TryUnlockNextScene()
        {
            bool isSuccess = false;

            //Debug.Log("curr:" + g_CurrSceneID + " and unlocked:" + ScenesUnlockedCount);
            if (g_CurrSceneID == ScenesUnlockedCount - 1)
            {
                int toUnlockID = g_CurrSceneID + 1;
                if (toUnlockID >= MaxScenes - 1)
                {
                    toUnlockID = MaxScenes - 1;
                }
                g_scenesUnlocked[toUnlockID] = true;
                isSuccess = true;
                //Debug.Log("one more scene unlocked at " + toUnlockID);
                SaveData();
            }
            else
            {
                isSuccess = false;
            }
            return isSuccess;
        }

        //比赛作弊用
        public static void CheatUnlockAllScenesForGameCompetion()
        {
            for (int i = 0; i < g_scenesUnlocked.Length; i++)
            {
                g_scenesUnlocked[i] = true;
            }

            SaveData();
        }

        /// <summary>
        /// 某个场景是否已经解锁
        /// </summary>
        /// <param name="sceneID"></param>
        /// <returns></returns>
        public static bool IsSceneUnlocked(int sceneID)
        {
            if (sceneID >= MaxScenes || sceneID < 0)
                return false;
            else
                return g_scenesUnlocked[sceneID];
        }

        /// <summary>
        /// 解锁特定的一个id的场景
        /// </summary>
        /// <param name="sceneID"></param>
        public static void UnlockEditMap(int sceneID)
        {
            //if (sceneID > 0 && sceneID < MaxScenes - 1)
            if(g_scenesUnlocked[sceneID]==false)
            {
                g_scenesUnlocked[sceneID] = true;
                
                SaveData();
            }
        }

        /// <summary>
        /// 是否存在下一场景(解锁的或者未解锁的）
        /// </summary>
        public static bool HasNextScene()
        {
            //新注释的Debug.Log ( "level count:" + Application.levelCount );
            return g_CurrSceneID != (MaxScenes - 1);
        }

        /// <summary>
        /// 读取下一个场景，如果已经达到最大场景了，则重玩最后一关
        /// </summary>
        public static void LoadNextScene()
        {
            g_CurrSceneID++;
            
            if (g_CurrSceneID >= MaxScenes)
            {
                Debug.Log("max level played. so replay again.");
                g_CurrSceneID = MaxScenes - 1;

            }

            SceneManager.LoadScene(g_CurrSceneID);
        }

        /// <summary>
        /// 重新读取当前游戏
        /// </summary>
        internal static void ReloadGame()
        {
            SceneManager.LoadScene(g_CurrSceneID);
        }
    }
}